﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Model;


namespace Control
{
    public class MGCCDs : ModuloDeGerenciamento<CCD>
    {
        public MGCCDs()
        {
            this.dc = new Model.DataClassesDataContext();
        }
        public override List<CCD> listar()
        {
            var ccds = from row in this.dc.CCDs select row;
            List<CCD> list = new List<CCD>();
            foreach (CCD c in ccds)
            {
                list.Add(c);
            }
            return list;
        }

        public override bool adicionar(CCD obj)
        {
            bool flag = true;
            try
            {
                this.dc.CCDs.InsertOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao inserir o registro";
                this.output += e.ToString();
                flag = false;
            }
            return flag;
        }
        public bool adicionar(String nome, String arquivo)
        {
            if (String.IsNullOrEmpty(nome) || String.IsNullOrEmpty(arquivo))
                return false;
            CCD c = new CCD();
            c.nome = nome;
            c.arquivo = arquivo;
            return this.adicionar(c);
        }
        public override bool remover(CCD obj)
        {
            bool flag = true;
            try
            {
                this.dc.CCDs.DeleteOnSubmit(obj);
                this.dc.SubmitChanges();
            }
            catch (Exception e)
            {
                this.output += "Erro ao remover o registro";
                this.output += e.ToString();
                flag = false;
            }
            return flag;
        }
        public override CCD get(uint id)
        {
            if (id == 0) return null;
            CCD ccd = null;
            try
            {
                ccd = this.dc.CCDs.Single(c => c.id == id);
            }
            catch (Exception e)
            {
                this.output += "Erro ao obter o registro";
                this.output += e.ToString();
                ccd = null;
            }
            return ccd;
        }

        public override bool remover(uint id)
        {
            return this.remover(this.get(id));
        }

    }
}
